package com.wp.controller;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wp.common.CURDUtil;
import com.wp.common.RequestUtil;
import com.wp.dto.AjaxDTO;

@Controller
public class CommonAjaxController {
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("commonAjax.do")
	public ModelAndView postRequest(HttpServletRequest req,HttpServletResponse resp){
		Map params = RequestUtil.getParams(req);
		JSONObject json = new JSONObject();
		
		try{
			if(StringUtils.isEmpty((String) params.get("sql"))){
				json.put("result","0");
				throw new NullArgumentException("查询sql参数为空");
			}else{
				List<AjaxDTO> dtoList = CURDUtil.queryList((String)params.get("sql"),params);
				json.put("result","1");
				json.put("resList", dtoList);
			}
			
			resp.setContentType("application/json; charset=utf-8");  
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			
		}catch(Exception e){
			return null;
		}
		return null;
	}
}
